package com.zr.d_c.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.d_c.model.Func;
import com.zr.d_c.service.FuncService;
import com.zr.d_c.serviceimpl.FuncServiceImpl;

public class AddFuncController extends HttpServlet {

	FuncService funcservice = new FuncServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Func> funcs = funcservice.getAllFunc();
		String fname = req.getParameter("fname");
		String fpath = req.getParameter("fpath");
		if(fname!=null){
			boolean flag = false;
			for (Func func : funcs) {
				if(func.getFname().equals(fname)){
					flag = true;
					break;
				}
			}
			if(!flag){
				Func f = new Func(fname,fpath,1,"open");
				funcservice.addFunc(f);
				req.setAttribute("msg", "添加成功");
				req.getRequestDispatcher("func/addfunc.jsp").forward(req, resp);
			}
			else{
				req.setAttribute("msg", "添加失败，已有该功能");
				req.getRequestDispatcher("func/addfunc.jsp").forward(req, resp);
			}
		}
	}
}
